package edu.byu.intex2.data.dao;

import java.sql.*;
import java.util.List;
import java.util.LinkedList;

/**
 * A singleton object that CRUD's Salesperson objects.
 *
 * @author Conan C. Albrecht <conan@warp.byu.edu>
 * @version 2008-03-03
 */
public class SalespersonDAO{
    
  /////////////////////////////////////////////
  ///   Singleton code
  
  private static SalespersonDAO instance = null;
  
  /** Creates a new instance of SalespersonDAO */
  private SalespersonDAO() {
  }//constructor

  /** Retrieves the single instance of this class */
  public static synchronized SalespersonDAO getInstance() {
    if (instance == null) {
      instance = new SalespersonDAO();
    }
    return instance;
  }//getInstance
  
  
  ////////////////////////////////////////////
  ///   CREATE methods
  
  /** Creates a new salesperson in the database */
  public Salesperson create() throws DataException {    
	//create a new blank salesperson object without any parameters in the constructor
	//set the alreadyInDB variable to false
	//put to cache and return the new object	
  }
  
  
  
  ////////////////////////////////////////////
  ///   READ methods
  
  /** Reads an existing salesperson from the database */
  public Salesperson read(String id) throws DataException {
    //check to see if the object is in cache, if it is just return it
	//if not call the create() method
	//establish a connection from the pool, then pass that connection along with the string id to the next method
	//catch any exceptions you may have
	//return the salesperson
	//RELEASE THE CONNECTION!!		
  }
  
  /** Internal method to read an existing salesperson from the database */
  synchronized Salesperson read(String id, Connection conn) throws Exception {
    //check the cache just like above just to make sure
	//use a prepared statement to prevent against sql injection
	//run the query and get the result set. have the next method process the result set
	//catch any exceptions you may have
	//return the salesperson
	
  }//read
  
  
  /** Internal method to create a salesperson object from a record */
  synchronized Salesperson readRecord(ResultSet rs, Connection conn) throws Exception {
    //again, check the cache, but check it with the id from the record set that was passed
	//create a new salesperson object. don't forget to put it to cache, set it's alreadyInDB param to true and dirty to false
	//return the salesperson
	
  }//readRecord
  

  
  /////////////////////////////////////////////
  ///   UPDATE methods
  
  /** Saves an existing salesperson in the database */
  public void save(Salesperson salesperson) throws DataException {
    //get a connection from the pool, save all of the sales first using the other salesperson method and then commit them in the database
	//rollback if there were any errors saving, catch any other errors you might have
	//release the connection!!
	
  }
  
  /** Internal method to update a salesperson in the database */
  void save(Salesperson salesperson, Connection conn) throws Exception {
    //check cache first, if returns nothing, put the salesperson object to cache
	//decide whether to update or insert into the database
	
    }  
  
  /** Saves an existing salesperson to the database */
  private void update(Salesperson salesperson, Connection conn) throws Exception {
    
	//use a prepared statement to prevent sql injection
	//run the query and set dirty to fase
	//make sure to close the prepared statement
	
  }
  
  /** Inserts a new salesperson into the database */
  private void insert(Salesperson salesperson, Connection conn) throws Exception {
    //use a perpared statement
	//set the alreadyInDB variable to true
	//dirty to false
	//close the prepared statement
	
  }  
  
  
  
  
  
  /////////////////////////////////////////////////
  ///   DELETE methods
  
  /** We do not support deleting of business objects in this application */
  public void delete(Salesperson salesperson) throws DataException {
    throw new RuntimeException("Nice try. The delete function is not supported in this application.");
  }
  

  
  ////////////////////////////////////////////////
  ///   SEARCH methods
  
  /** Retrieves all dayofweeks from the database */
  public List<Salesperson> getAll() throws DataException {
    //establish a connection from the pool, use prepared statement, use the search method below to run the statement, catch any errors
	//release the connection
	
  }
  
  /** Internal method to search by certain fields */
  private List<Salesperson> search(PreparedStatement stmt, Connection conn) throws Exception {
    //instantiate a list of sales to add recordsets to. this will make going through the list easier
	//finally, close the statement and return the list
	
  }
  /*this is in a list because we may have more than one salesperson with the same name, just allow the user to pick one since it is an exception*/
  public List<Salesperson> getByName(String fname, String lname) throws DataException{
	//establish connection, prepared statement call the search
  }
  
   public Salesperson getBylength(int months) throws DataException{
	//establish connection, prepared statement call the search
	//call the read(rs.GUID) and return the result
  } 
  
  
}//class
